#include"common.hpp"

extern Log log;

int main(){
    Init init; // 创建管道

    int shimid = CreateShm(); // 创建共享区
    log(DEBUG,"create shm done");
    // sleep(5);

    int fd = open(FIFO_FILE,O_RDONLY); // 读取管道
    if(fd<0)
    {
        log(FATAL,"error string:%s, error code %d",strerror(errno),errno);
        exit(FIFO_OPEN_ERROR);
    }

    char* shmaddr = (char*)shmat(shimid,nullptr,0);
    log(DEBUG,"attach shm done, shmaddr: 0x%x",shmaddr);

    struct shmid_ds shmds;
    while(true)
    {
        // char buffer[1024];
        // memcpy(buffer,shmaddr,sizeof(buffer) + 1);
        char c;
        ssize_t s = read(fd,&c,1);  // 只读到c才去读贡献区
        if(s==0) break;
        else if(s<0) break;

        printf("client say:");
        cout<<shmaddr<<endl;
        
        shmctl(shimid, IPC_STAT,&shmds); // 将属性读到结构体中
        cout<<"shm size："<< shmds.shm_segsz<<endl; // 读取属性


    }

    shmdt(shmaddr);
    log(DEBUG,"detach shm done, shmaddr: 0x%x",shmaddr);


    shmctl(shimid,IPC_RMID,nullptr);
    log(DEBUG,"delete shm done");

    close(fd);


    return 0;
}